<?php


namespace app\api\controller;

use app\api\services\HtnsAdminService;
use app\api\services\HtnsApiService;
use app\api\services\HtnsService;
use app\api\services\QrcodeService;
use think\Exception;
use think\facade\Lang;

class Htns extends Base
{
    /**
     * @desc 获取商品参与活动列表
     * @return \think\response\Json
     */
    public function activityList()
    {
        try{
            $param = input();
            $type = empty($param['type']) ? 'api' : 'admin';

            $service = new HtnsAdminService();
            $ret = $service->activityList($type);

            if($type == 'admin'){
                return $this->returnResult(1 , '' ,$ret);
            }
            return $this->returnResult(200 , '' ,$ret);
        }catch (Exception $e){
            return $this->returnResult(500 , $e->getTrace());
        }
    }
    /**
     * @desc 获取跳转商品详情页二维码
     * @return \think\response\Json
     */
    public function productQrcode()
    {
        try{
            $param = input();
            $id = empty($param['id']) ? 0 : $param['id'];
            if(!$id){
                return $this->returnResult(400 , '商品id不能为空' );
            }
            $qrcodeService = new QrcodeService();
            $data['type'] = 'scan';
            $data['page'] = '/pages/goods_details/index?id='.$id;
            $data['data']['product_id'] = $id;
            $ret = $qrcodeService->makeQrcode($data);

            return $this->returnResult(200 , '获取成功!' ,$ret);
        }catch (Exception $e){
            return $this->returnResult(500 , $e->getTrace());
        }
    }
    /**
     * @desc 生成小程序二维码
     * @return \think\response\Json
     */
    public function xcxQrcode()
    {
        try{
            $param = input();//请求参数 page:页面路径,str:参数

            $qrcodeService = new QrcodeService();
            $page = $param['page'];//页面路径
            $dataStr = $param['dataStr'];//参数
            $fileName = 'coupon_type_2_'.date('YmdHis').'.png';//图片名称
            $ret = $qrcodeService->makeXcxUnlimitQrcode($page, $dataStr ,$fileName);
            return $this->returnResult(200 , '获取成功!' ,$ret);
        }catch (Exception $e){
            return $this->returnResult(500 , $e->getTrace());
        }
    }
}